package io.output;

import org.junit.Test;

import java.io.*;
import java.util.concurrent.Semaphore;

/**
 * @author mubei
 * @date 2021/12/3
 * @description
 */
public class ObjectOutStream {

    @Test()
    public void test1() throws Exception {
        String filePath = "D:\\date\\distable\\practice\\code\\basic\\src\\main\\java\\io/data.data";
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filePath));


        // 序列化
        oos.writeInt(100);
        oos.writeBoolean(true);
        oos.writeChar('a');
        oos.writeDouble(9.7);
        Dog dog = new Dog();
        oos.writeObject(dog);
        oos.close();
    }

    @Test()
    public void test2() throws Exception {
        String filePath = "D:\\date\\distable\\practice\\code\\basic\\src\\main\\java\\io/data.data";
         ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filePath));
        // 反序列化   读取需要注意顺序
        System.out.println(ois.readInt());
        System.out.println(ois.readBoolean());
        System.out.println(ois.readChar());
        System.out.println(ois.readDouble());
        System.out.println(ois.readObject());

        ois.close();

    }

    @Test()
    public void test3() throws Exception {
        String filePath = "D:\\date\\distable\\practice\\code\\basic\\src\\main\\java\\io/data.data";
        File file = new File(filePath);

        file.delete();
    }
}

class Dog implements Serializable{
    int age = 10;
    String name = "旺财";
}
